package com.css.zfzx.sjcj.modules.hddcCjHistorypoints.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.common.utils.UpdateUtil;
import com.css.zfzx.sjcj.modules.hddcCjHistorypoints.repository.HddcCjHistorypointsNativeRepository;
import com.css.zfzx.sjcj.modules.hddcCjHistorypoints.repository.HddcCjHistorypointsRepository;
import com.css.zfzx.sjcj.modules.hddcCjHistorypoints.repository.entity.HddcCjHistorypointsEntity;
import com.css.zfzx.sjcj.modules.hddcCjHistorypoints.service.HddcCjHistorypointsService;
import com.css.zfzx.sjcj.modules.hddcCjHistorypoints.viewobjects.HddcCjHistorypointsQueryParams;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangping
 * @date 2020-12-23
 */
@Service
public class HddcCjHistorypointsServiceImpl implements HddcCjHistorypointsService {

	@Autowired
    private HddcCjHistorypointsRepository hddcCjHistorypointsRepository;
    @Autowired
    private HddcCjHistorypointsNativeRepository hddcCjHistorypointsNativeRepository;

    @Override
    public JSONObject queryHddcCjHistorypointss(HddcCjHistorypointsQueryParams queryParams, int curPage, int pageSize,String sort,String order) {
        Page<HddcCjHistorypointsEntity> hddcCjHistorypointsPage = this.hddcCjHistorypointsNativeRepository.queryHddcCjHistorypointss(queryParams, curPage, pageSize, sort, order);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcCjHistorypointsPage);
        return jsonObject;
    }


    @Override
    public HddcCjHistorypointsEntity getHddcCjHistorypoints(String id) {
        HddcCjHistorypointsEntity hddcCjHistorypoints = this.hddcCjHistorypointsRepository.findById(id).orElse(null);
         return hddcCjHistorypoints;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcCjHistorypointsEntity saveHddcCjHistorypoints(HddcCjHistorypointsEntity hddcCjHistorypoints) {
        String uuid = UUIDGenerator.getUUID();
        hddcCjHistorypoints.setUuid(uuid);
        if(StringUtils.isEmpty(hddcCjHistorypoints.getCreateUser())){
            hddcCjHistorypoints.setCreateUser(PlatformSessionUtils.getUserId());
        }
        hddcCjHistorypoints.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcCjHistorypointsRepository.save(hddcCjHistorypoints);
        return hddcCjHistorypoints;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcCjHistorypointsEntity updateHddcCjHistorypoints(HddcCjHistorypointsEntity hddcCjHistorypoints) {
        HddcCjHistorypointsEntity entity = hddcCjHistorypointsRepository.findById(hddcCjHistorypoints.getUuid()).get();
        UpdateUtil.copyNullProperties(entity,hddcCjHistorypoints);
        hddcCjHistorypoints.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcCjHistorypoints.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcCjHistorypointsRepository.save(hddcCjHistorypoints);
        return hddcCjHistorypoints;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcCjHistorypointss(List<String> ids) {
        List<HddcCjHistorypointsEntity> hddcCjHistorypointsList = this.hddcCjHistorypointsRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcCjHistorypointsList) && hddcCjHistorypointsList.size() > 0) {
            for(HddcCjHistorypointsEntity hddcCjHistorypoints : hddcCjHistorypointsList) {
                this.hddcCjHistorypointsRepository.delete(hddcCjHistorypoints);
            }
        }
    }


}
